/**
 * Created by zy on 2016/6/13.
 * 阴保整改 - 维修整改 - 腐蚀控制
 */

;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    // 阴保表格
    var $datatable_cath = $('#JQ_datatable_cath'), DTCATH;
    // 导入文件input
    var $export_name = $('#JQ_export_name');

    // 筛选对象
    var filter = {
        // 搜索
        getSearch: function (param) {
            var data = {};
            data.mode = true;
            // 分页参数
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.pipeSegmentId = function () {
                var v = $segment.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.beginMileage = function () {
                var v = $begin.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            data.endMileage = function () {
                var v = $end.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            return data
        }
    };

    DTCATH = $datatable_cath.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(corrctrlApi.url_cath_list(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
        },
        'columns': [
            {
                data: 'pipeLineName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'fromPointName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'toPointName', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'name', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'assemblyCompany', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'planStartDate', render: function (data) {
                var date = Dyy.getDate(data);
                return '<span title="' + date + '">' + date + '</span>';
            }
            },
            {
                data: 'id', render: function (data,type,row) {
                if(row.includeReport == true){
                    return '<i title="上传" class="zy-file-upload-icon" data-id="' + data + '"></i>&nbsp;&nbsp;&nbsp;'
                        + '<i title="下载" class="zy-file-download-icon" data-id="' + data + '" data-name="'+row.name+'"></i>';
                }else{
                    return '<i title="上传" class="zy-file-upload-icon" data-id="' + data + '"></i>&nbsp;&nbsp;&nbsp;'
                        + '<i title="暂无可下载项" class="zy-file-nodownload-icon" data-id="' + data + '" data-name="'+row.name+'"></i>';
                }
            }
            },
            {
                data: 'id', render: function (data) {
                return '<i title="导入" class="zy-file-import-icon" data-id="' + data + '"></i>&nbsp;&nbsp;&nbsp;'
                    + '<a href="' + DyyBase.root_path + '/corrctrl/CathRepairTask/initDetail.html?repairTaskId=' + data + '"><i title="查看" class="zy-file-eyes-icon" data-id="' + data + '"></i></a>';
            }
            },
            {
                data: 'id', render: function (data, type, row) {
                if(row.dataStated == '20'){
                    return '<i title="不可编辑" class="zy-record-add-icon disabled" data-id="' + data + '"></i>&nbsp;&nbsp;&nbsp;'
                        + '<i title="不可删除" class="zy-list-icon-delete disabled" data-id="' + data + '"></i>';
                }
                return '<a href="' + DyyBase.root_path + '/corrctrl/CathRepairTask/initCreate.html?repairTaskId=' + data + '"><i title="编辑" class="zy-record-add-icon" data-id="' + data + '"></i></a>&nbsp;&nbsp;&nbsp;'
                    + '<i title="删除" class="zy-list-icon-delete" data-id="' + data + '"></i>';
            }
            }
        ]
    });


    // 搜索
    $('#JQ_search').click(function () {
        if (DTCATH) {
            DTCATH.draw();
        }
    });

    // 删除
    $datatable_cath.on('click', 'i.zy-list-icon-delete', function () {
        var $this = $(this);
        if($this.hasClass('disabled')){
            $this.MSG(
                {
                    type: 'error',
                    content: '删除失败,已导入维修建议的阴保整改项目不可删除!'
                }
            );
        }else{
            $this.MSG({
                height: 230,
                type: 'confirm',
                content: '您确认要删除此阴保整改项目?',
                yesBtnTxt: '确定',
                yesBtnAct: function () {
                    var id = $this.attr('data-id');
                    DyyBase.ajax(corrctrlApi.url_cath_delete(), {repairTaskId: id}, function (dataset) {
                        if (dataset.bool) {
                            var count = dataset.result.successCount ? dataset.result.successCount : 0;
                            if (count) {
                                $this.MSG({
                                    type: 'success',
                                    content: '删除' + count + '条阴保整改项目成功!',
                                    yesBtnAct: DyyBase.pageReload,
                                    noBtnAct: DyyBase.pageReload
                                });
                            } else {
                                $this.MSG({
                                    type: 'error',
                                    content: '删除此阴保整改项目失败!'
                                });
                            }
                        } else {
                            $this.MSG({
                                type: 'error',
                                content: dataset.result
                            });
                        }
                    });
                }
            });
        }
    });

    // 不可编辑
    $datatable_cath.on('click', 'i.zy-record-add-icon.disabled', function (){
        $(this).MSG(
            {
                type: 'error',
                content: '编辑失败,已导入维修建议的阴保整改项目不可编辑!'
            }
        );
    });

    // 导入
    $datatable_cath.on('click', 'i.zy-file-import-icon', function () {
        var $this = $(this);
        DyyBase.isLogin(function(){
            DyyBase.importFile({
                button: $this,
                title: '导入整改记录',
                input: $('#JQ_export_name'),
                file: $('#JQ_bower_file'),
                form: $('#JQ_cath_form_file'),
                iframe: $('#JQ_cath_iframe'),
                box: 'div.zy-import-record',
                yes: 'JQ_action_yes_btn',
                no: 'JQ_action_no_btn',
                param: {repairTaskId: $this.attr('data-id')}
            });
        });
    });

    // 上传
    $datatable_cath.on('click', 'i.zy-file-upload-icon', function () {
        var $this = $(this);
        DyyBase.isLogin(function(){
            var id = $this.attr('data-id');
            $this.UPW({
                fileType: 'CP_REC_RECODE_REPORT',
                businessId: id,
                suffix: {title: 'Excel files', extensions: 'xls,xlsx,doc,docx,pdf'}
            });
        });
    });

    // 下载
    $datatable_cath.on('click', 'i.zy-file-download-icon', function () {
        var $this = $(this);
        DyyBase.isLogin(function(){
            var $form = $('#JQ_download_form').empty();
            var $iframe = $('#JQ_download_iframe').empty();
            var postObj = {
                businessId: $this.attr('data-id'),
                downFileName: $this.attr('data-name') + '阴保整改报告',
                type: 'CP_REC_RECODE_REPORT',
                ymd: true
            };
            for(var k in postObj){
                var $input = $('<input type="hidden">');
                $input.prop('name', k).val(postObj[k]);
                $form.append($input);
            }
            $form.submit();
            $iframe.get(0).onload = function(){
                try{
                    var content = this.contentWindow.document.body.innerHTML;
                    content = $.parseJSON(content);
                    if(content.bool){

                    }else{
                        $this.MSG({
                            type: 'error',
                            content: content.result
                        });
                    }
                }catch(e){
                    $this.MSG({
                        type: 'error',
                        content: e.message
                    });
                }
            }
        });
    });

})(jQuery, window, document);